package 遍历作业;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.GroupMatcher;

import java.util.Date;
import java.util.List;

/**
 * create at 2018/10/17
 * author: suveng
 * email: suveng@163.com
 **/
public class Excemple {
    public static void main(String[] args) throws SchedulerException {

        JobKey jobKey = new JobKey("ja", "suveng");
        JobKey jobKey1 = new JobKey("jb", "suveng2");
        JobKey jobKey2 = new JobKey("jc", "suvengc");

        JobDetail joba= JobBuilder.newJob(JobA.class).withIdentity(jobKey).build();

        JobDetail jobb= JobBuilder.newJob(JobB.class).withIdentity(jobKey1).build();

        JobDetail jobc=JobBuilder.newJob(JobC.class).withIdentity(jobKey2).build();

        CronTrigger aTriger = TriggerBuilder.newTrigger().withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
                .withIdentity(new TriggerKey("ta", "suveng")).build();
        CronTrigger bTrigger = TriggerBuilder.newTrigger().withIdentity(new TriggerKey("tb", "suveng"))
                .withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?")).build();
        CronTrigger cTrigger = TriggerBuilder.newTrigger().withIdentity(new TriggerKey("tc", "suveng"))
                .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")).build();

        Scheduler scheduler = new StdSchedulerFactory().getScheduler();

        scheduler.getListenerManager().addJobListener(new JobListener() {
            @Override
            public String getName() {
                return "suveng";
            }

            @Override
            public void jobToBeExecuted(JobExecutionContext jobExecutionContext) {
                System.out.println("执行任务之前------------");
            }

            @Override
            public void jobExecutionVetoed(JobExecutionContext jobExecutionContext) {

            }

            @Override
            public void jobWasExecuted(JobExecutionContext jobExecutionContext, JobExecutionException e) {
                System.out.println("执行任务之后----------------");
                Scheduler scheduler1 = jobExecutionContext.getScheduler();
                System.out.println("-----------------遍历");
                try {
                    for (String groupName : scheduler1.getJobGroupNames()) {
                        System.out.println("==============");

                        for (JobKey jobKey : scheduler1.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) {

                            String jobName = jobKey.getName();
                            String jobGroup = jobKey.getGroup();

                            //get job's trigger
                            List<Trigger> triggers = (List<Trigger>) scheduler1.getTriggersOfJob(jobKey);
                            Date nextFireTime = triggers.get(0).getNextFireTime();

                            System.out.println("[jobName] : " + jobName + " [groupName] : "
                                    + jobGroup + " - " + nextFireTime);

                        }

                    }
                } catch (SchedulerException e1) {
                    e1.printStackTrace();
                }
            }
        });
        scheduler.start();
        scheduler.scheduleJob(joba,aTriger);
        scheduler.scheduleJob(jobb,bTrigger);
        scheduler.scheduleJob(jobc,cTrigger);
    }
}
